// pages/all-list/all-list.js
var util = require('../../utils/util.js');
var app = getApp();
Page({
  data:{
    page:1,
    limit:20,
    t:13,
    uid:'',
    source:"",
    data:[],
    imgDomain: app.globalData.imgDomain,
    hasMore: true,
    hasLoadding: false
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    var source = options.source;
    var uid = options.uid;
    var t,navtitle;
    if(source == "zyzl"){
       t = 8;
       navtitle = "重要展览";
    }
    else if(source == "ysnb"){
       t = 13;
       navtitle = "艺术年表";
    }
    else if(source == "ryjx"){
       t = 14;
       navtitle = "荣誉奖项";
    }
    else if(source == "scpm"){
       t = 15;
       navtitle = "收藏拍卖";
    }
    else if(source == "gyjz"){
       t = 16;
       navtitle = "公益捐赠";
    }
    else if(source == "cbzz"){
       t = 18;
       navtitle = "出版刊登";
    }
    wx.setNavigationBarTitle({
      title: navtitle
    });      
    this.setData({
      t: t,
      uid: uid,
      source: source,
      data:[]
    });
    
    this.load();
  },
  load: function(){
    this.setData({
      hasLoadding:true
    });
    var that = this;
    var source = this.data.source;
    util.api({
        data:{
            publickey:util.publiccode,
            ac:"topiclist",
            postuid:this.data.uid,
            topictype:this.data.t,
            toporder:1,
            page:this.data.page,
            num:this.data.limit
        },
        success:function(responseData){
          var data = responseData.data;
          var newData;
          if(data.msg){
            that.setData({
              hasMore:false
            });
          }else{
            if(source == "ysnb" || source == "ryjx"){
              for(var i=0 ; i< data.length ; i++){
                  var age = data[i].age.split("-");
                  data[i].ageYear = age[0];
                  if (age[0] && age[1]) data[i].ageMonth = age[1]+"-"+age[2];
                  data[i].message = data[i].message.replace(/\n/g, '\n\n');
                  data[i].message = data[i].message.replace(/&nbsp;/g, '');
              }
            }
            newData = that.data.data.concat(data);
            that.setData({
              data:newData,
              hasLoadding:false
            });
          }
        }
    });
  },
  loadMore: function(){
    var page;
    if(this.data.hasMore && !this.data.hasLoadding){
      page = this.data.page;
      this.setData({
        page: ++page
      });
      this.load();
    }
  },
  goAtherPage: function (e) {
    var url = e.currentTarget.dataset.link;
    wx.navigateTo({ url: url })
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  }
})